﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BigLotterySyndicate.Secured.Users.Controls
{
    public partial class TabGeneral : Base
    {
        protected string NameValue
        {
            get { return this.txtUserName.Text; }
            set { this.txtUserName.Text = value; }
        }              

        protected string EmailValue
        {
            get { return this.txtEmail.Text; }
            set { this.txtEmail.Text = value; }
        }

        protected bool EnabledValue
        {
            get { return Boolean.Parse(this.ddlEnabled.SelectedValue); }
            set { this.ddlEnabled.SelectedValue = value ? "True" : "False"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if (!Page.IsPostBack)
                SetForm();
        }

        private void SetForm()
        {
            this.NameValue = CurrentUser.username;
            this.EmailValue = CurrentUser.email;
            this.EnabledValue = CurrentUser.isEnabled;
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;


            ShowResult(SaveForm());
        }

        private void ShowResult(bool p)
        {
            InsertEventLogEntry("User [" + CurrentUser.ToString() + "] account details updated.");

            this.PlaceHolderSuccess.Visible = p;
            this.PlaceHolderFailure.Visible = !this.PlaceHolderSuccess.Visible;
        }

        private bool SaveForm()
        {
            CurrentUser.username = this.NameValue;
            CurrentUser.email = this.EmailValue;
            CurrentUser.isEnabled = this.EnabledValue;
            return this.BasePage.BusinessFacade.UserBL.UpdateUser();
        }

        protected void ButtonAddFunds_Click(object sender, EventArgs e)
        {
            this.BasePage.Redirect(String.Format("/Secured/Payments/Create.aspx?uId={0}", CurrentUser.id));
        }

        #region print

        protected string PrintDrawsEntered()
        {
            return this.BasePage.BusinessFacade.PlayerBL.GetDrawsByPlayerId(CurrentUser.id).Count().ToString();
        }

        protected string PrintFundsRemaining()
        {
            return String.Format("&pound;{0}.00", this.CurrentUser.funds);
        }

        #endregion
    }
}